package hellogwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class HelloGWT implements EntryPoint {

	RequestBuilder requestB;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		Image img = new Image(
				"http://code.google.com/webtoolkit/logo-185x175.png");
		Button button = new Button("Click me");

		VerticalPanel vPanel = new VerticalPanel();
		// We can add style names.
		vPanel.addStyleName("widePanel");
		vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		vPanel.add(img);
		vPanel.add(button);

		// Add image and button to the RootPanel
		RootPanel.get().add(vPanel);

		// Create the dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Welcome to GWT!");
		dialogBox.setAnimationEnabled(true);
		Button closeButton = new Button("close");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setWidth("100%");
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		dialogVPanel.add(closeButton);

		closeButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				dialogBox.hide();
			}
		});

		// Set the contents of the Widget
		dialogBox.setWidget(dialogVPanel);

		button.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				dialogBox.center();
				dialogBox.show();
			}
		});

		// 定义发送的请求
		requestB = new RequestBuilder(RequestBuilder.GET, "./test.html");
		// 请求的回复对象
		RequestCallback back = new RequestCallback() {
			public void onError(Request request, Throwable exception) {

			}

			public void onResponseReceived(Request request, Response response) {
				dialogBox.setText(response.getText());

			}
		};

		// 发送请求
		try {
			requestB.sendRequest(null, back);
		} catch (RequestException e) {
			e.printStackTrace();
		}
	}

}
